package com.kaily.imagelib.element;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.text.Layout;
import android.text.StaticLayout;
import android.text.TextPaint;
import android.util.Log;

import com.kaily.imagelib.IMergeImageElement;

/**
 * 文本元素
 *
 * @author kaily_zhou
 * @date 2018/10/19 下午2:04
 */
public class TextElement implements IMergeImageElement {
    private final String TAG = "TextElement";
    private String text;
    private float textSize;
    private int textColor;
    private int left;
    private int top;
    private int align;
    private int width;
    private int height;
    private boolean isRequireElement;
    private StaticLayout titleLayout;
    private boolean isBoldFont;

    public TextElement(String text, float textSize, int textColor,
                       int left, int top, int align, int width, boolean isRequireElement) {
        this.text = text;
        this.textSize = textSize;
        this.textColor = textColor;
        this.left = left;
        this.top = top;
        this.align = align;
        this.width = width;
        this.isRequireElement = isRequireElement;
    }

    public TextElement(String text, float textSize, int textColor,
                       int left, int top, int align, int width, boolean isRequireElement, boolean isBoldFont) {
        this(text, textSize, textColor,
                left, top, align, width, isRequireElement);
        this.isBoldFont = isBoldFont;
    }

    @Override
    public boolean generateElement() {
        try {
            Paint paint = new Paint();
            TextPaint textpaint = new TextPaint(paint);
            textpaint.setTextSize(textSize);
            textpaint.setFakeBoldText(isBoldFont);
            textpaint.setAntiAlias(true);
            textpaint.setColor(textColor);
            Layout.Alignment alignment = Layout.Alignment.ALIGN_NORMAL;
            if (this.align == 1) {
                alignment = Layout.Alignment.ALIGN_NORMAL;
            } else if (this.align == 2) {
                alignment = Layout.Alignment.ALIGN_CENTER;
            } else if (this.align == 3) {
                alignment = Layout.Alignment.ALIGN_OPPOSITE;
            }
            titleLayout = new StaticLayout(text, textpaint,
                    width, alignment, 1f, 0f, false);
            height = titleLayout.getHeight();
            Log.d(TAG, ">>>>" + height);
            return true;
        } catch (Exception e) {

            return !isRequireElement;
        }

    }

    public int getHeight() {
        return height;
    }

    @Override
    public boolean mergeImage(Canvas canvas) {
        if (canvas == null) {
            return false;
        }
        if (titleLayout == null) {
            return !isRequireElement;
        }
        try {
            canvas.save();
            canvas.translate(left, top);
            titleLayout.draw(canvas);
            canvas.restore();
            return true;
        } catch (Exception e) {
            return !isRequireElement;
        }
    }

    @Override
    public boolean isRequireElement() {
        return isRequireElement;
    }
}
